<?php


namespace app\sports\events\service;

use think\Db;
use think\Exception;
use app\common\HTTP;
use app\common\OSS;
use think\facade\Log;
use app\sports\events\model\EventsModel;
use app\common\InstanceTrait;
class EventsCaiji
{
    private $user = '';
    private  $secret ='';
//    接口名字:足球赛事资料
    private $url = 'open.sportnanoapi.com/api/sports/football/matchevent/list';

  protected function __construct()
  {
        $this->user = Env::get('nami.user');
        $this->secret = Env::get('nami.secret');
  }

  /*
   * 赛事类型列表采集,只跑一次,更新的跑后面update
   */
  public function Execs(){
    try{
        //获取远程的数据
         $file_get_date= json_decode(HTTP::getInstance()->doGet($this->url."?user=$this->user&secret=$this->secret"),true);
        //遍历每一条赛事的信息
          $eventsdata=[];
         foreach ($file_get_date['matchevents'] as $item){
             $item['events_oss'] = OSS::getInstance()->uploadStream('',$item['logo'],true);
            $eventsdata[]=[
                'events_id' => $item['id'],
                'name_zh'   => $item['name_zh'],
                'short_name_zh' =>$item['short_name_zh'],
                'type'      =>$item['type'],
                'logo'      => $item['events_oss'],
                'create_time'  =>date('Y-m-d H:i:s',time()),
                'update_time'  =>date('Y-m-d H:i:s',time()),
            ];
        }
         $eventsresult=EventsModel::getInstance()->InsertEventsAll($eventsdata);
         if (!$eventsresult) throw new Exception("赛事列表采集失败");
         Log::log('info','赛事采集成功');
    }catch(Exception $e){
         Log::error($e->getMessage());
         return [];
    }
  }

    /*
     * 更新赛事数据
     */
  public function Update(){
        try{
            //获取远程最新的数据
          $file_get_date= json_decode(HTTP::getInstance()->doGet($this->url."?user=$this->user&secret=$this->secret"),true);
    //      查询数据库现有的数据
          $file_get_id =[];
            foreach ($file_get_date['matchevents'] as $item){
                $file_get_id[] = $item['id'];
            }
            $model = EventsModel::getInstance()->QueryEventsId();

            $newid = array_diff($file_get_id,$model);
    //        批量添加
            $eventsdata=[];
            foreach ($file_get_date['matchevents'] as $value){
                if (in_array($value['id'],$newid)){
    //                这里是就是需要添加的数据
                    $eventsdata[]=[
                        'events_id' => $value['id'],
                        'name_zh'   => $value['name_zh'],
                        'short_name_zh' =>$value['short_name_zh'],
                        'type'      =>$value['type'],
                        'logo'      => $value['events_oss'],
                        'create_time'  =>date('Y-m-d H:i:s',time()),
                        'update_time'  =>date('Y-m-d H:i:s',time()),
                    ];
                }else{
    //                需要更新的数据
                    $data=[
                        'events_id' => $value['id'],
                        'name_zh'   => $value['name_zh'],
                        'short_name_zh' =>$value['short_name_zh'],
                        'type'      =>$value['type'],
                        'update_time'  =>date('Y-m-d H:i:s',time()),
                    ];
                    EventsModel::getInstance()->UpdateEvents($value['id'],$data);
                }
            }

            if (isset($eventsdata) && (!empty($eventsdata))){
                $eventsresult=EventsModel::getInstance()->InsertEventsAll($eventsdata);
            }
            Log::log('info','赛事更新成功');
        }catch(Exception $e){
            Log::error('赛事更新错误:'.$e->getMessage());
            return [];
        }
  }


}